﻿@page "/UPdatadetecPage"
@inject DetedctcacheServer detedctcacheServer
@using System.ComponentModel
@using AntDesign.TableModels
@inject ModalService _modalService
@inject MessageService _message

<Table @ref="table"
       TItem="DetectCache_Entity"
       DataSource="@forecasts"
       @bind-SelectedRows="selectedRows">
    <Column @bind-Field="@context.id" Sortable Hidden="@true"/>
    <Column Title="说明" Field="@context.Remark">
        <Input @bind-Value=@context.Remark Disabled="@true"/>
    </Column>
    <Column Title="是否有料" Field="@context.IsHave">
        <Switch @bind-Value="@context.IsHave"></Switch>
    </Column>
    <ActionColumn>
        <Button @onclick="()=>OnChange(context)">更新</Button>
    </ActionColumn>

</Table>

<br />



@using System.Text.Json;
@code {

    ObservableCollection<DetectCache_Entity> forecasts = new ObservableCollection<DetectCache_Entity>();

    IEnumerable<DetectCache_Entity> selectedRows;
    ITable table;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;

    protected override async Task OnInitializedAsync()
    {
        var lists = await detedctcacheServer.Getlist();
        if (lists != null)
        {
            forecasts.Clear();
            foreach (var item in lists)
            {
                forecasts.Add(item);
            }
        }

    }

    async Task init()
    {
        forecasts.Clear();
        var List =  await detedctcacheServer.Getlist();
        if (List != null)
        {
            await Task.Run(() =>
            {

                foreach (var item in List)
                {
                    forecasts.Add(item);
                }
                this.InvokeAsync(() =>
                {
                    this.StateHasChanged();
                });
            });
        }


    }





    public async void OnChange(DetectCache_Entity detectCache_Entity)
    {
        bool flag = await detedctcacheServer.UPdata(detectCache_Entity);
        if (flag)
        {
            await _message.Info($"CODE{detectCache_Entity.Deviceid}-更新成功");
        }
        else
        {
            await _message.Info($"CODE{detectCache_Entity.Deviceid}-更新失败");
        }
        await init();
    }
}

